<?php
require_once 'Zend/Controller/Action.php';

class AlbumController extends Zend_Controller_Action {

	public function preDispatch(){
	    
	    $auth = Zend_Auth::getInstance();
	    if(!$auth->hasIdentity()){
	        
	        $this->_redirect('/auth/login');
	    }
	}
	
    public function init(){

		$this->initView();
		Zend_Loader::loadClass('Album');
		$this->view->user = Zend_Auth::getInstance()->getIdentity();
		
	}

	public function indexAction(){

		$this->view->title = "My Albums";
		$album = new Album();
		$this->view->albums = $album->fetchAll();

	}

	public function addAction(){

		$this->view->title = "Add New Album";

		if($this->_request->isPost()){
				
			Zend_Loader::loadClass('Zend_Filter_StripTags');
			$filter = new Zend_Filter_StripTags();
				
			$artist = $filter->filter($this->_request->getPost('artist'));
			$artist = trim($artist);
			$title = trim($filter->filter($this->_request->getPost('title')));
				
			if($artist != '' && $title != ''){

				$data = array(
				     'artist' => $artist,
				     'title'  => $title,
				);
				$album = new Album();
				$album->insert($data);

				$this->_redirect('/album');
				return;
			}
		}

		//set up an "empty" album
		$album = new Album();
		$this->view->album = $album->createRow();

		//additional view fields required by form
		$this->view->action = 'add';
		$this->view->buttonText = 'Add';

	}

	public function editAction(){

		$this->view->title = "Edit Album";
		$album = new Album();

		if($this->_request->isPost()){
				
			Zend_Loader::loadClass('Zend_Filter_StripTags');
			$filter = new Zend_Filter_StripTags();
				
			$id = (int)$this->_request->getPost('id');
			$artist = $filter->filter($this->_request->getPost('artist'));
			$artist = trim($artist);
			$title = trim($filter->filter($this->_request->getPost('title')));
				
			if($id !== false){
				if($artist != '' && $title != ''){
					$data = array(
					     'artist' => $artist,
					     'title'  => $title,
					);
					$where = 'id = '.$id;
					$album->update($data, $where);
						
					$this->_redirect('/album');
				}else{
					$this->view->album = $album->fetchRow('id = '.$id);
				}
			}
		}else{
			//album id should be $params['id']
			$id = (int)$this->_request->getParam('id', 0);
			if($id > 0){
				$this->view->album = $album->fetchRow('id= '.$id);
			}
		}
		// addtional view fields required by form
		$this->view->action = 'edit';
		$this->view->buttonText = 'Update';
	}



	public function deleteAction(){

		$this->view->title = "Delete Album";
		
		$album = new Album();
		if($this->_request->isPost()){
			
			Zend_Loader::loadClass('Zend_Filter_Alpha');
			$filter = new Zend_Filter_Alpha();
			$id = (int)$this->_request->getPost('id');
			$del = $filter->filter($this->_request->getPost('del'));
			
			if($del == 'Yes' && $id > 0){
				
				$where = 'id = '.$id;
				$rows_affected = $album->delete($where);
			}
		}else{
			$id = $this->_request->getParam('id');
			if($id > 0){
				//only render if we have an id and can find the album.
				$this->view->album = $album->fetchRow('id = '.$id);
				
				if($this->view->album->id > 0){
					//render template automatically.
					return;
				}
			}
		}
		//redirect back to the album list
		$this->_redirect('/album');
	}
}

?>